from datetime import datetime
from typing import List
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_stock.service.weekly_service import WeeklyService
from module_stock.entity.vo.weekly_vo import DeleteWeeklyModel, WeeklyModel, WeeklyPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


weeklyController = APIRouter(prefix='/stock/weekly', dependencies=[Depends(LoginService.get_current_user)])


@weeklyController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:list'))]
)
async def get_stock_weekly_list(
    request: Request,
weekly_page_query: WeeklyPageQueryModel = Depends(WeeklyPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    weekly_page_query_result = await WeeklyService.get_weekly_list_services(query_db, weekly_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=weekly_page_query_result)

@weeklyController.get(
    '/all',
    response_model=List[WeeklyModel],
    dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:all'))],
)
async def get_all_stock_weekly(request: Request, symbol: str, query_db: AsyncSession = Depends(get_db)):
    # 获取全量数据
    weekly_list_result = await WeeklyService.get_all_weekly_services(query_db, symbol)
    logger.info('获取成功')

    return ResponseUtil.success(data=weekly_list_result)


@weeklyController.post('', dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:add'))])
@ValidateFields(validate_model='add_weekly')
@Log(title='股票周线', business_type=BusinessType.INSERT)
async def add_stock_weekly(
    request: Request,
    add_weekly: WeeklyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_weekly.create_time = datetime.now()
    add_weekly.update_time = datetime.now()
    add_weekly_result = await WeeklyService.add_weekly_services(query_db, add_weekly)
    logger.info(add_weekly_result.message)

    return ResponseUtil.success(msg=add_weekly_result.message)


@weeklyController.put('', dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:edit'))])
@ValidateFields(validate_model='edit_weekly')
@Log(title='股票周线', business_type=BusinessType.UPDATE)
async def edit_stock_weekly(
    request: Request,
    edit_weekly: WeeklyModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_weekly.update_by = current_user.user.user_name
    edit_weekly.update_time = datetime.now()
    edit_weekly_result = await WeeklyService.edit_weekly_services(query_db, edit_weekly)
    logger.info(edit_weekly_result.message)

    return ResponseUtil.success(msg=edit_weekly_result.message)


@weeklyController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:remove'))])
@Log(title='股票周线', business_type=BusinessType.DELETE)
async def delete_stock_weekly(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_weekly = DeleteWeeklyModel(ids=ids)
    delete_weekly_result = await WeeklyService.delete_weekly_services(query_db, delete_weekly)
    logger.info(delete_weekly_result.message)

    return ResponseUtil.success(msg=delete_weekly_result.message)


@weeklyController.get(
    '/{id}', response_model=WeeklyModel, dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:query'))]
)
async def query_detail_stock_weekly(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    weekly_detail_result = await WeeklyService.weekly_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=weekly_detail_result)


@weeklyController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('stock:weekly:export'))])
@Log(title='股票周线', business_type=BusinessType.EXPORT)
async def export_stock_weekly_list(
    request: Request,
    weekly_page_query: WeeklyPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    weekly_query_result = await WeeklyService.get_weekly_list_services(query_db, weekly_page_query, is_page=False)
    weekly_export_result = await WeeklyService.export_weekly_list_services(weekly_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(weekly_export_result))
